home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / LIB / CHDTR.C < prev    next >
C/C++ Source or Header  |  1995-10-03  |  4KB  |  219 lines

  1. /* ======= */
  2. /* chdtr.c */
  3. /* ======= */
  4. /*
  5. Cephes Math Library Release 2.0:  April, 1987
  6. Copyright 1984, 1987 by Stephen L. Moshier
  7. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  8. */
  9. /* ====================================================================    *
  10.  * chdtrc()                                *
  11.  *                                    *
  12.  *    Complemented Chi-square distribution  (Right Tail Area, x,inf]) *
  13.  *                                    *
  14.  * chdtr()                                *
  15.  *                                    *
  16.  *    Chi-square distribution  (Left Tail Area, [0,x])        *
  17.  *                                    *
  18.  * chdtri()                                *
  19.  *                                    *
  20.  *    Inverse of complemented Chi-square distribution         *
  21.  *                                    *
  22.  *    Given Degrees of Freedom and Percentage,            *
  23.  *    This Function Yields Chi-Square Value                *
  24.  * ====================================================================    */
  25.  
  26. /* ====================================================================    */
  27. /* chdtrc()
  28.  *
  29.  *    Complemented Chi-square distribution
  30.  *
  31.  *
  32.  *
  33.  * SYNOPSIS:
  34.  *
  35.  * double v, x, y, chdtrc();
  36.  *
  37.  * y = chdtrc( v, x );
  38.  *
  39.  *
  40.  *
  41.  * DESCRIPTION:
  42.  *
  43.  * Returns the area under the right hand tail (from x to
  44.  * infinity) of the Chi square probability density function
  45.  * with v degrees of freedom:
  46.  *
  47.  *
  48.  *                    inf.
  49.  *                      -
  50.  *              1         | |  v/2-1  -t/2
  51.  *  P( x | v )     =   -----------     |     t    e     dt
  52.  *              v/2  -       | |
  53.  *             2      | (v/2)   -
  54.  *                     x
  55.  *
  56.  * where x is the Chi-square variable.
  57.  *
  58.  * The incomplete gamma integral is used, according to the
  59.  * formula
  60.  *
  61.  *    y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
  62.  *
  63.  *
  64.  * The arguments must both be positive.
  65.  *
  66.  *
  67.  *
  68.  * ACCURACY:
  69.  *
  70.  * See igamc().
  71.  *
  72.  * ERROR MESSAGES:
  73.  *
  74.  *   message         condition        value returned
  75.  * chdtrc domain  x < 0 or v < 1    0.0
  76.  */
  77. /* ====================================================================    */
  78. #include "mconf.h"
  79.  
  80. double chdtrc(df,x)
  81. double df, x;
  82. {
  83. double igamc();
  84.  
  85. if( (x < 0.0) || (df < 1.0) )
  86.     {
  87.     mtherr( "chdtrc", DOMAIN );
  88.     return(0.0);
  89.     }
  90. return( igamc( df/2.0, x/2.0 ) );
  91. }
  92.  
  93. /* ====================================================================    */
  94. /* chdtr()
  95.  *
  96.  *    Chi-square distribution
  97.  *
  98.  *
  99.  *
  100.  * SYNOPSIS:
  101.  *
  102.  * double df, x, y, chdtr();
  103.  *
  104.  * y = chdtr( df, x );
  105.  *
  106.  *
  107.  *
  108.  * DESCRIPTION:
  109.  *
  110.  * Returns the area under the left hand tail (from 0 to x)
  111.  * of the Chi square probability density function with
  112.  * v degrees of freedom.
  113.  *
  114.  *
  115.  *                    inf.
  116.  *                      -
  117.  *              1         | |  v/2-1  -t/2
  118.  *  P( x | v )     =   -----------     |     t    e     dt
  119.  *              v/2  -       | |
  120.  *             2      | (v/2)   -
  121.  *                     x
  122.  *
  123.  * where x is the Chi-square variable.
  124.  *
  125.  * The incomplete gamma integral is used, according to the
  126.  * formula
  127.  *
  128.  *    y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
  129.  *
  130.  *
  131.  * The arguments must both be positive.
  132.  *
  133.  *
  134.  *
  135.  * ACCURACY:
  136.  *
  137.  * See igam().
  138.  *
  139.  * ERROR MESSAGES:
  140.  *
  141.  *   message         condition        value returned
  142.  * chdtr domain   x < 0 or v < 1    0.0
  143.  */
  144. /* ====================================================================    */
  145.  
  146. double chdtr(df,x)
  147. double df, x;
  148. {
  149. double igam();
  150. double Answer;
  151. if( (x < 0.0) || (df < 1.0) )
  152.     {
  153.     mtherr( "chdtr", DOMAIN );
  154.     return(0.0);
  155.     }
  156.  
  157. Answer = igam( df/2.0, x/2.0 );
  158. return( Answer );
  159. }
  160.  
  161. /* ====================================================================    */
  162. /* chdtri()
  163.  *
  164.  *    Inverse of complemented Chi-square distribution
  165.  *
  166.  *
  167.  *
  168.  * SYNOPSIS:
  169.  *
  170.  * double df, x, y, chdtri();
  171.  *
  172.  * x = chdtri( df, y );
  173.  *
  174.  *
  175.  *
  176.  *
  177.  * DESCRIPTION:
  178.  *
  179.  * Finds the Chi-square argument x such that the integral
  180.  * from x to infinity of the Chi-square density is equal
  181.  * to the given cumulative probability y.
  182.  *
  183.  * This is accomplished using the inverse gamma integral
  184.  * function and the relation
  185.  *
  186.  *    x/2 = igami( df/2, y );
  187.  *
  188.  *
  189.  *
  190.  *
  191.  * ACCURACY:
  192.  *
  193.  * See igami.c.
  194.  *
  195.  * ERROR MESSAGES:
  196.  *
  197.  *   message         condition        value returned
  198.  * chdtri domain   y < 0 or y > 1     0.0
  199.  *               v < 1
  200.  *
  201.  */
  202. /* ====================================================================    */
  203.  
  204. double chdtri( df, y )
  205. double df, y;
  206. {
  207. double x;
  208. double igami();
  209.  
  210. if( (y < 0.0) || (y > 1.0) || (df < 1.0) )
  211.     {
  212.     mtherr( "chdtri", DOMAIN );
  213.     return(0.0);
  214.     }
  215.  
  216. x = igami( 0.5 * df, y );
  217. return( 2.0 * x );
  218. }
  219.